using System;

namespace Exif.Tiff
{
	///<summary>
	///
	///</summary>
	public class TagTypeSizeAttribute : Attribute
	{
		///<summary>
		///
		///</summary>
		///<param name="bytes"></param>
		public TagTypeSizeAttribute(UInt32 bytes)
		{
			this.Bytes = bytes;
		}

		///<summary>
		///
		///</summary>
		public UInt32 Bytes { get; private set; }

		///<summary>
		///
		///</summary>
		///<param name="type"></param>
		///<returns></returns>
		public static UInt32 GetTagSize(TagType type)
		{
			var attr = (TagTypeSizeAttribute)GetAttribute(type, typeof(TagTypeSizeAttribute));
			if(attr != null)
			{
				return attr.Bytes;
			}

			return 0;
		}

		public static Attribute GetAttribute<TEnum>(TEnum enumValue, Type attributeType)
		{
			//if (enumValue == null)
			//{
			//    throw new ArgumentNullException("enumValue");
			//}

			if (attributeType == null)
			{
				throw new ArgumentNullException("attributeType");
			}

			var enumType = enumValue.GetType();

			if(!enumType.IsEnum)
			{
				throw new ArgumentException();
			}

			var enumFieldName = Enum.GetName(enumType, enumValue);
			var fieldInfo = enumType.GetField(enumFieldName);

			var attributes = fieldInfo.GetCustomAttributes(attributeType, true);
			if (attributes != null && attributes.Length > 0)
			{
				return attributes[0] as Attribute;
			}

			return null;
		}
	}
}